package com.wind.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018/8/21.
 */
public class FileUtil {


    /**
     *@Description: 复制文件
     *@param: oldFile-源文件，newFile目标文件
     *@return:
     *@author:longwj
     *@Date:2018/8/21
     *@Time:18:07
     */
    public static void copyFile(String oldFile,String newFile){
        deleteFile(newFile);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(oldFile);
            fos = new FileOutputStream(newFile,true);
            byte[] words = new byte[1024];
            while (fis.read()!=-1){
                fis.read(words);
                fos.write(words,0,words.length);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    /**
     *@Description:删除单个文件
     *@param:被删文件路径
     *@return:
     *@author:longwj
     *@Date:2018/8/21
     *@Time:18:11
     */
    public static void deleteFile(String filePath){
        File file = new File(filePath);
        if(file.isFile() && file.exists()){
            file.delete();
        }
    }

}
